package ils.domain.issue.model;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class ItemRepositoryImpl implements ItemRepository
{
    private static final String GET_ITEM_BARCODE = "select item from Item as item where item.barcode = ?";
    private SessionFactory sessionFactory;
    @Override
    public Item getItem(String barcode)
    {
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(GET_ITEM_BARCODE);
        query.setString(0, barcode);
        return (Item)query.uniqueResult();
    }

    @Override
    public Item getItem(int id) {
        Session session = sessionFactory.getCurrentSession();
        return (Item)session.get(Item.class, id);
    }

    @Override
    public Item createItem(Item item)
    {
        Session session = sessionFactory.getCurrentSession();
        session.save(item);
        return item;
    }
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

}
